package com.zzxx.controller;

import com.github.pagehelper.PageInfo;
import com.zzxx.domain.Role;
import com.zzxx.domain.UserInfo;
import com.zzxx.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/findAll")
    public String findAll(Model model,
                          @RequestParam(name = "pageNum", required = true, defaultValue = "1") Integer pageNum,
                          @RequestParam(name = "pageSize", required = true, defaultValue = "4") Integer pageSize){
        List<UserInfo> list = userInfoService.findAll(pageNum, pageSize);
        PageInfo<UserInfo> userPageInfo = new PageInfo<>(list);
        model.addAttribute("userPageInfo", userPageInfo);
        return "user-list";
    }

    @RequestMapping("/{id}")
    public String findByUid(@PathVariable("id") String uid, Model model){
        UserInfo userInfo = userInfoService.findByUid(uid);
        model.addAttribute("userInfo", userInfo);
        return "user-detail";
    }

    @RequestMapping("/add")
    public String add(UserInfo userInfo){
        userInfoService.insert(userInfo);
        // 页面跳转
        return "redirect:/user/findAll";
    }

    @RequestMapping("/findOtherRolesById")
    public String findOtherRolesById(@RequestParam(name = "id") String id, Model model){
        // 根据用户id找到用户没有的角色信息
        List<Role> list = userInfoService.findOtherRolesById(id);
        model.addAttribute("list", list);
        model.addAttribute("userId", id);
        return "user-add-role";
    }

    @RequestMapping("/addRole")
    public String addRole(String[] ids,String userId){
        userInfoService.addRole(ids, userId);
        return "redirect:/user/findAll";
    }
}
